package com.example.plan.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.plan.common.PageRequest;
import com.example.plan.common.result.Result;
import com.example.plan.model.constnant.DroneEnums;
import com.example.plan.model.dto.order.OrderDTO;
import com.example.plan.model.entity.Order;
import com.example.plan.model.entity.UserAddress;
import com.example.plan.model.vo.OrderVO;
import com.example.plan.model.vo.UserAddressVO;
import com.example.plan.service.OrderService;
import com.example.plan.mapper.OrderMapper;
import com.example.plan.utils.BeanPageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @author ASUS
 * @description 针对表【order(订单表)】的数据库操作Service实现
 * @createDate 2025-03-01 19:07:17
 */
@Service
@Slf4j
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order>
        implements OrderService {

    @Override
    public Result<Order> createOrder(Order order) {
        Order order1 = BeanUtil.copyProperties(order, Order.class);
        return baseMapper.insert(order1) > 0 ? Result.success("保存成功") : Result.failed("保存失败");
    }

    @Override
    public Result<Void> updateOrder(OrderDTO order) {
        Order orderEntity = BeanUtil.copyProperties(order, Order.class);
        boolean isUpdated = baseMapper.updateById(orderEntity) > 0;
        if (isUpdated) {
            return Result.success("更新订单成功");
        } else {
            return Result.failed("更新订单失败");
        }
    }


    @Override
    public Result<Void> deleteOrder(Long orderId) {

        try {
            // 执行删除操作
            boolean isDeleted = baseMapper.deleteById(orderId) > 0;

            if (isDeleted) {
                log.info("订单ID {} 删除成功", orderId);
                return Result.success();
            } else {
                log.warn("订单ID {} 删除失败，可能不存在该订单", orderId);
                return Result.failed();
            }
        } catch (Exception e) {
            // 异常处理
            log.error("删除订单时发生异常，订单ID: {}", orderId, e);
            return Result.failed();
        }
    }

    @Override
    public Result<Order> getOrder(Long orderId) {
        return null;
    }

}




